/**
 * 
 */
package com.paypal.melearning.ui.component;

import com.paypal.melearning.domain.Comment;
import com.paypal.melearning.domain.ContentType;
import com.paypal.melearning.domain.Pin;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.event.MouseEvents.ClickListener;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

/**
 * @author prakrishnan
 * 
 */
public class PinComponent extends VerticalLayout {

	private Pin curPin;
	
	@Override
	public void attach() {
		super.attach();
		
		setWidth("100%");
		setMargin(true);
		setSpacing(true);
		
		Panel pinPannel = new Panel();
		Label pinName = new Label(curPin.getPinName());
//		pinName.setWidth("200px");		
		pinPannel.addComponent(pinName);

		Embedded content = new Embedded();
		content.setWidth("180px");
		addPinResource(curPin, content);
		addPinListner(curPin, content);
		pinPannel.addComponent(content);
		
		Label pinDescription = new Label(curPin.getDescription());
//		pinName.setWidth("200px");
		pinPannel.addComponent(pinDescription);
		
		VerticalLayout commentsPannel = new VerticalLayout();
		addComments(curPin,commentsPannel);
	
		addComponent(pinPannel);
		addComponent(commentsPannel);
	}


	public PinComponent(Pin curPin) {
		this.curPin = curPin;
	}


	private void addPinResource(Pin pin, Embedded content) {

		Resource pinSrc = new ClassResource("/doc-icon.jpg", getApplication());
		if (ContentType.DOCUMENT.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ClassResource("/doc-icon.jpg", getApplication());
		} else if (ContentType.PPT.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ClassResource("/ppt-icon.jpg", getApplication());
		} else if (ContentType.PDF.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ClassResource("/pdf-icon.jpg",
					getApplication());
		} else if (ContentType.WEB.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ExternalResource(pin.getContentURL());
			content.setType(Embedded.TYPE_BROWSER);
		} else if (ContentType.IMAGE.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ExternalResource(pin.getContentURL());
		}else if (ContentType.VIDEO.name().equalsIgnoreCase(pin.getType())) {
			pinSrc = new ExternalResource(pin.getContentURL());
			content.setMimeType("application/x-shockwave-flash");
			content.setParameter("allowFullScreen", "true");
			content.setHeight("265px");

		}
		
		content.setSource(pinSrc);
	}
	
	
	private void addPinListner(final Pin pin, final Embedded content) {

		content.addListener(new ClickListener() {

			private static final long serialVersionUID = -7541319743055222613L;

			public void click(ClickEvent event) {
				content.getWindow().open(
						new ExternalResource(pin.getContentURL()), "_blank");

			}
		});
		
	}
	
	private void addComments(Pin pin, VerticalLayout commentsPannel) {
		for(Comment comment : pin.getComment()){
			Label commentLabel = new Label(comment.getCommentedBy().getUserName() + "-" + comment.getComment());
			commentsPannel.addComponent(commentLabel);
		}
	}


}
